package com.xms.sdk.client;

import com.xms.schema.entity.Attribute;
import com.xms.schema.entity.Entity;
import com.xms.schema.entity.RelationShip;
import com.xms.schema.service.attribute.IAttributeFinderService;
import com.xms.schema.service.relationship.IRelationShipFinderService;
import com.xms.sdk.core.query.AttributeAggregateExpression;
import com.xms.sdk.data.IFieldValueUpdater;
import com.xms.sdk.service.IFormulaUpdater;
import com.xms.utils.CollectionUtil;
import com.xms.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * FormulaUpdater
 * </p>
 *
 * @author migo
 * @since 2021/10/17
 */
@Service
public class FormulaUpdater implements IFormulaUpdater {
    private IFieldValueUpdater _fieldValueUpdater;
    private IAttributeFinderService _attributeFinderService;
    private IRelationShipFinderService _relationShipFinderService;
    @Autowired
    public FormulaUpdater(
            IFieldValueUpdater fieldValueUpdater
            , IAttributeFinderService attributeFinderService
            , IRelationShipFinderService relationShipFinderService
    )
    {
        _fieldValueUpdater = fieldValueUpdater;
        _attributeFinderService = attributeFinderService;
        _relationShipFinderService = relationShipFinderService;
    }

    @Override
    public boolean update(Entity entityMetadata, com.xms.core.data.Entity data) throws SQLException {
        //exists summary field?
        List<Attribute> summaryFields = _attributeFinderService.getBySummaryEntityId(entityMetadata.getEntityid());
        if (CollectionUtil.notEmpty(summaryFields))
        {
            for (Attribute item : summaryFields)
            {
                AttributeAggregateExpression ae = JsonUtil.toBean(item.getSummaryexpression(), AttributeAggregateExpression.class);
                if (!data.containsKey(ae.Field))
                {
                    continue;
                }
                if (ae.EntityName.equalsIgnoreCase(data.Name))
                {
                    RelationShip rs = _relationShipFinderService.getByName(ae.RelationshipName);
                    _fieldValueUpdater.updateSummaryValue(entityMetadata, data, item, rs, ae);
                    //字段公式计算
                    List<Attribute> formulaAttributes = _attributeFinderService.getByEntityName(rs.getReferencedentityname()).stream().filter(n -> n.getValuetype() == 2).collect(Collectors.toList());
                    if (CollectionUtil.notEmpty(formulaAttributes))
                    {
                        formulaAttributes.add(_attributeFinderService.getById(rs.getReferencedattributeid()));
                        _fieldValueUpdater.updateFormulaValue(rs.getReferencedentityname(), formulaAttributes, data.getUUIDValue(rs.getReferencedattributename()));
                    }
                }
            }
        }
        return true;
    }
}
